import { mars3d, Cesium, widget, graphicLayer, map, store } from "@/utils/global";
import { getToolDefaultStyles, getUserStyles } from "@/api/tool";
/**
 * 搜索控件 异步查询
 */
export const querySearchAsync = (queryString: string, cb: (arg: any) => void) => {
    if (!queryString) return;
    const queryPoi = new mars3d.query.GaodePOI({ key: ["9bcb1e0d9dca902457a23f5332d8a102"] });
    queryPoi.queryText({
        text: queryString,
        page: 0,
        count: 30,
        success: (res: any) => {
            cb(res.list);
        },
    });
};
/**
 * 搜索控件 同步查询 选中后回调
 */
export const handleSelect = (item: Record<string, any>, callBack: Function) => {
    // 搜索框赋值
    addgraphic(item);
    callBack(item.name);
};
// 添加搜索点标记
const addgraphic = (item: any) => {
    let graphic: any = graphicLayer.getGraphicById("0000000000");
    if (graphic) {
        deletegraphic(graphic);
    }
    graphic = new mars3d.graphic.BillboardEntity({
        position: [item.lng, item.lat],
        style: {
            image: "/files/mapicon/mark-blue.png",
            scale: 0.4,
            horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
            verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
            clampToGround: true,
        },
        id: "0000000000",
    });
    graphicLayer.addGraphic(graphic);
    // 添加标记点信息
    graphicLayer.bindPopup(function (event: any) {
        const attr = event.graphic.attr || {};
        attr["地址"] = `${item.xzqh}${item.address}`;
        attr["电话"] = item.tel;
        attr["经度"] = item.lng;
        attr["纬度"] = item.lat;
        attr["备注"] = item.type;

        return mars3d.Util.getTemplateHtml({
            title: item.name,
            template: "all",
            attr,
        });
    });
    map.flyToGraphic(graphic, { radius: 1500 });
    // bindLayerContextMenu();
};
// const bindLayerContextMenu = () => {
//     graphicLayer.bindContextMenu([
//         {
//             text: "删除标记点",
//             icon: "fa fa-trash-o",
//             callback: (e: any) => {
//                 let graphic = e.graphic;
//                 if (graphic) {
//                     graphicLayer.removeGraphic(graphic);
//                 }
//             },
//         },
//     ]);
// };
// 删除搜索点标记
const deletegraphic = (graphic: any) => {
    graphicLayer.removeGraphic(graphic);
};
/**
 * 下拉菜单 点击事件
 * @param command
 */

export const handleCommand = (command: any) => {
    if (command.type == "print") {
        widget.print.show = true;
    } else {
        widget.adobeIllustrator = true;
        widget.adobeIllustratorData = command;
        getToolDefaultStyles({
            type: command.type,
        }).then((res: any) => {
            let data = res.data;
            widget.adobeIllustratorItems = data;
            getCustomStyles(command);
        });
    }
};
/**
 * 获取自定义样式
 */
const getCustomStyles = (command: any) => {
    getUserStyles({
        type: command.type,
    }).then((draws: any) => {
        const data = draws.data.map((item: any) => {
            item.checked = false;
            return item;
        });
        widget.customStyleItem = data;
    });
};
